package cn.ixinjiu.libraryMangement.controller.systemAdmin.anno;

import cn.ixinjiu.libraryMangement.entity.Anno;
import cn.ixinjiu.libraryMangement.service.IAnnoService;
import cn.ixinjiu.libraryMangement.service.impl.AnnoServiceImpl;
import com.alibaba.fastjson2.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by XinChen on 2023-06-11
 *
 * @TODO 新增公告
 */
@WebServlet("/annoAdd")
public class AnnoAddServlet extends HttpServlet {
    private IAnnoService annoService = new AnnoServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置编码格式
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-Type", "text/html;charset=utf-8");

        // 获取前端参数
        String content = req.getParameter("content");
        Anno anno = new Anno();
        anno.setContent(content);

        // 调用新增方法
        int add = annoService.annoAdd(anno);
        // 返回数据
        JSONObject json = new JSONObject();
        if (add == 1) {
            json.put("code", "0");
            json.put("msg", "修改成功");
        } else {
            json.put("code", "1");
            json.put("msg", "修改失败");
        }
        // 输出到页面
        PrintWriter writer = resp.getWriter();
        writer.write((json.toString()));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
